package com.g001.enums;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;

import com.g001.util.CachedResourceLoader;

public enum CharacterType {
 
	
	SPIDER(0, "data/g001/devoIDLE.png", "data/g001/devoHURT.png","data/g001/devoATTACK.png", 2000, true),
	SQUELETON(1, "data/g001/devoIDLE.png", "data/g001/devoHURT.png","data/g001/devoATTACK.png",2500, true),
	EVIL_ERIC(2, "data/g001/devoIDLE.png", "data/g001/devoHURT.png","data/g001/devoATTACK.png",200, true),
	ERIC(3, "data/g001/devoIDLE.png", "data/g001/devoHURT.png", "data/g001/devoATTACK.png",100, false),
	;
	
	
	
	// animation
	private String ANIM_IDLE = "IDLE";
	private String ANIM_HURT = "HURT";
	private String ANIM_ATTACK = "ATTACK";
	

	public final static int TILEWIDTH = 160;
	public final static int TILEHEIGHT = 200;
	
	private final static int NBFRAMEPERLINE = 5;
	private final static int NBTOTALFRAMES = 21;
	
	private final static int NBFRAMEPERLINE_HURT = 2;
	private final static int NBTOTALFRAMES_HURT = 5;
	
	private final static int NBFRAMEPERLINE_ATTACK = 4;
	private final static int NBTOTALFRAMES_ATTACK = 17;
	
	
	private Map<String, Animation> animations = null;
	private final static Random random = new Random();
	
	
	private int id;
	private Image[] idle = new Image[NBTOTALFRAMES];
	private Image[] hurt = new Image[NBTOTALFRAMES_HURT];
	private Image[] attack = new Image[NBTOTALFRAMES_ATTACK];
	private Animation idleAnim = null;
	private Animation hurtAnim = null;
	private Animation attackAnim = null;
	
	// life
	private int life;
	private boolean right2left;
	
	private CharacterType(final int id, final String idleAnimation, final String hurtAnimation, final String attackAnimation, final int life, final boolean right2left) {
		this.id = id;
		this.right2left = right2left;
		int x = 0;
		int y = 0;
		this.life = life;
		for(int nbPic = 1;nbPic<=NBTOTALFRAMES; nbPic++) {
			idle[nbPic-1] = CachedResourceLoader.getInstance().getImage(idleAnimation).getSubImage(x,y, TILEWIDTH, TILEHEIGHT);
			x += TILEWIDTH;
			if(nbPic%NBFRAMEPERLINE == 0 && nbPic!=0){
				x = 0;
				y += TILEHEIGHT;
			}
		}
		x = 0;
		y = 0;
		for(int nbPic = 1;nbPic<=NBTOTALFRAMES_HURT; nbPic++) {
			hurt[nbPic-1] = CachedResourceLoader.getInstance().getImage(hurtAnimation).getSubImage(x,y, TILEWIDTH, TILEHEIGHT);
			x += TILEWIDTH;
			if(nbPic%NBFRAMEPERLINE_HURT == 0 && nbPic!=0){
				x = 0;
				y += TILEHEIGHT;
			}
		}
		x = 0;
		y = 0;
		for(int nbPic = 1;nbPic<=NBTOTALFRAMES_ATTACK; nbPic++) {
			attack[nbPic-1] = CachedResourceLoader.getInstance().getImage(attackAnimation).getSubImage(x,y, TILEWIDTH, TILEHEIGHT);
			x += TILEWIDTH;
			if(nbPic%NBFRAMEPERLINE_ATTACK == 0 && nbPic!=0){
				x = 0;
				y += TILEHEIGHT;
			}
		}
		idleAnim = new Animation(idle,100,true);
		idleAnim.setPingPong(true);		
		hurtAnim = new Animation(hurt,100,true);
		hurtAnim.setPingPong(true);
		attackAnim = new Animation(attack,100,true);
		attackAnim.setPingPong(true);
		if(right2left) {
			idleAnim = flipAnimation(idleAnim);
			hurtAnim = flipAnimation(hurtAnim);
			attackAnim = flipAnimation(attackAnim);
		}
		animations = new HashMap<String, Animation>();
		animations.put(ANIM_IDLE, idleAnim);
		animations.put(ANIM_HURT, hurtAnim);
		animations.put(ANIM_ATTACK, attackAnim);
	}

	public int getId() {
		return id;
	}

	public static CharacterType getBlockType(final int id) {
		switch(id) {
			case 0 : return SPIDER;
			case 1 : return SQUELETON;
			case 2 : return EVIL_ERIC;
			case 3 : return ERIC;
			default :throw new IllegalArgumentException("no id ("+id+") affected to any enemy type");
		}
	}
	
	public static CharacterType getRandomEnemyType(){
		return getBlockType(1+random.nextInt(CharacterType.values().length)); 
	}

	public Animation getIdleAnim() {
		return animations.get(ANIM_IDLE);
	}
	
	public Animation getHurtAnim() {
		return animations.get(ANIM_HURT);
	}
	
	public int getLife(){
		return life;
	}
	
	public static Animation flipAnimation(Animation anim){
		Animation newAnim = new Animation();
		newAnim.setSpeed(anim.getSpeed());
		for(int i = 0; i<anim.getFrameCount(); i++){
			newAnim.addFrame(anim.getImage(i).getFlippedCopy(true, false)
					, anim.getDuration(i)); 
		}
		return newAnim;
	}

	public Animation getShieldAnim() {
		// TODO Auto-generated method stub
		return animations.get(ANIM_IDLE);
	}

	public Animation getMagicAnim() {
		// TODO Auto-generated method stub
		return animations.get(ANIM_IDLE);
	}

	public Animation getCurseAnim() {
		// TODO Auto-generated method stub
		return animations.get(ANIM_IDLE);
	}

	public Animation getAttackAnim() {
		// TODO Auto-generated method stub
		return animations.get(ANIM_ATTACK);
	}

	public boolean isRight2left() {
		return right2left;
	}
 

}
